风格开关实现
主要是体现语义化
代码实现
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>iOS 开关组件</title>
<style>
/* 外层容器 */
.ios-switch {
position: relative;
width: 52px;
height: 32px;
display: inline-block;
}
/* 原生复选框隐藏 */
.ios-switch input {
opacity: 0;
width: 0;
height: 0;
}
/* 开关轨道(label充当背景轨道) */
.ios-switch label {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #e5e5e5;
border-radius: 32px;
transition: background 0.3s ease;
}
/* 圆形滑块 */
.ios-switch label::before {
content: "";
position: absolute;
height: 28px;
width: 28px;
left: 2px;
bottom: 2px;
background-color: white;
border-radius: 50%;
box-shadow: 0 1px 3px rgba(0,0,0,0.2);
transition: transform 0.3s ease;
}
/* 选中态:轨道变绿色 */
.ios-switch input:checked + label {
background-color: #34c759; /* iOS原生绿色 */
}
/* 选中态:滑块右移 */
.ios-switch input:checked + label::before {
transform: translateX(20px);
}
</style>
</head>
<body>
<!-- 你面试题目标准结构:div包裹input + label -->
<div class="ios-switch">
<input type="checkbox" id="switch">
<label for="switch"></label>
</div>
</body>
</html>